/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-10-14
 * Time:13:52
 */

//无头双向链表模拟实现
public class MyLinkedList implements IList{
    //创建节点
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //确定链表的头节点和尾节点的位置
    public ListNode head;
    public ListNode last;

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = last = node;
        }else {
            node.next = head;//将node头插到head节点前
            head.prev = node;//更新head的prev引用指向node
            head = node;//更新node为新的头节点
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = last = node;
        }else {
            last.next = node;//将node尾插到last尾节点之后
            node.prev = last;//更新node的prev引用指向last
            last = last.next;//更新node为新的尾节点
        }
    }

    //判断index位置是否合法
    private void checkIndex(int index) throws IndexIllegalException{
        if (index < 0 || index >size()) {
            throw new IndexIllegalException("index位置不合法！");
        }
    }
    //寻找index位置的节点
    private ListNode findIndexOfNode(int index) {
        ListNode cur = head;
        //cur移动到index目标位置节点
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    @Override
    public void addIndex(int index, int data) {
        try {
            checkIndex(index);
            if (index == 0) {
                addFirst(data);
                return;
            }
            if (index == size()) {
                addLast(data);
                return;
            }
            ListNode node = new ListNode(data);
            ListNode cur = findIndexOfNode(index);//找到要增加节点的目标位置index
            node.next = cur;//先将node的next指向cur目标节点
            cur.prev.next = node;//接着将cur的前一个节点的next指向node
            node.prev = cur.prev;//然后将node的prev指向cur的前一个节点
            cur.prev = node;//最后将cur的prev指向node节点
        }catch (IndexIllegalException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null) {
            if (cur.val == key) {
                //开始删除
                if (cur == head) { //如果是删除头节点，将原来的head向后走，
                    head = head.next;
                    if (head != null) {
                        head.prev = null;//然后将此时的head的prev引用改为null，表示删除头节点成功
                    }
                }else {
                    cur.prev.next = cur.next;//先将要删除的cur节点的前一个节点的next改为指向cur的下一个节点
                    if (cur.next == null) {
                        last = last.prev;//如果这个cur的下一个节点是null，表示该cur是尾节点，直接将last向前移动一位，表示删除尾节点成功
                    }else {
                        cur.next.prev = cur.prev;//然后将cur的下一个节点的prev改为指向cur的前一个节点，表示删除cur成功
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        last = last.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = last = null;
    }
}
